package com.firmboy.spring_axis2.config;


import org.apache.axis2.transport.http.AxisServlet;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import static org.apache.axis2.deployment.WarBasedAxisConfigurator.PARAM_AXIS2_REPOSITORY_PATH;

/**
 * @author firmboy
 * @create 2018-06-06 下午7:25
 **/
@Configuration
public class Axis2Config {

    @Bean
    public ServletRegistrationBean axisServlet(){

        //String repositoryPath = String.format("%sWEB-INF", getClass().getResource("/").getPath());
        ServletRegistrationBean servlet = new ServletRegistrationBean();
        servlet.setServlet(new AxisServletExport());
        servlet.addUrlMappings("/services/*");
        servlet.setName("AxisServlet");
        servlet.setLoadOnStartup(1);
        //servlet.addInitParameter(PARAM_AXIS2_REPOSITORY_PATH, repositoryPath);
        return servlet;
    }




    public SimpleServletConfig simpleServletConfig(ServletContext servletContext){
        String repositoryPath = String.format("%sWEB-INF", getClass().getResource("/").getPath());
        InputStream is = null;
        BufferedInputStream inputStream = null;
        try {
            System.out.println("repositoryPath="+repositoryPath+"/services.xml");
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream("WEB-INF/services.xml");
            System.out.println("测试开始");
            if(is==null){
                System.out.println("测试失败");
            }
            inputStream = new BufferedInputStream(is);
            byte[] bytes = new byte[1024];
            inputStream.read(bytes,0,1024);
            String s = new String(bytes);
            System.out.println("s=========="+s);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {

        }


        return  new SimpleServletConfig(servletContext);
    }

}
